<!--
 * @Author: your name
 * @Date: 2022-03-02 23:31:03
 * @LastEditTime: 2022-03-04 00:08:28
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \video\pages\login\login.vue
-->
<template>
	<view class="page-login">
		<image class="logo" src="../../static/logo.jpg" mode="widthFix"></image>
		<view class="login-box">
			<button class="login-btn" type="primary" @click="getusrinfo">
				授权登录
			</button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
	// const { loginUrl } = require("../../utils/interfaceUrl.js");
	import {
		getShopInfo
	} from "@/api/index";
	export default {
		data() {
			return {
				js_code: ""
			};
		},
		onLoad() {
			console.log("onLoad -> uni.getStor", uni.getStorageSync("token"));
			if (uni.getStorageSync("token").length > 0) {
				uni.redirectTo({
					url: "/pages/index/index"
				}); //如果存在token 和authtoken 就是出于登录状态，直接进入首页
			}
		},

		methods: {
			getusrinfo() {
				var self = this;

				uni.showLoading({
					mask: true,
					title: "正在登录···",
					complete: () => {}
				});
				// 获取用户信息
				uni.getUserProfile({
					lang: "zh_CN",
					desc: "登录",
					success: function(infoRes) {
						uni.login({
							provider: "weixin",
							success: function(loginRes) {
								console.log("login -> loginRes", loginRes);
								let js_code = loginRes.code; //js_code可以给后台获取unionID或openID作为用户标识
								//infoRes里面有用户信息需要的话可以取一下
								let username = infoRes.userInfo.nickName; //用户名
								let gender = infoRes.userInfo.gender; //用户性别
								let avatarUrl = infoRes.userInfo.avatarUrl;
								let formdata = {
									code: js_code,
									nickname: username,
									sex: gender,
									picture: avatarUrl
								};
								//login是接口地
								// self.$refs.uToast.show({
								//   type: "error",
								//   icon: false,
								//   title: "失败主题12312321312312312312312312312321",
								//   message: "一弦一柱思华年",
								//   iconUrl:
								//     "https://cdn.uviewui.com/uview/demo/toast/error.png"
								// });
								console.log("login -> formdata", formdata);
								getShopInfo(formdata).then(res => {
									if (res.code == 200) {
										uni.setStorageSync("token", res.data.token);
										// self.$refs.uToast.show({
										//   type: "error",
										//   icon: false,
										//   title: "失败主题",
										//   message: "一弦一柱思华年",
										//   iconUrl:
										//     "https://cdn.uviewui.com/uview/demo/toast/error.png"
										// });

										uni.redirectTo({
											url: "/pages/index/index"
										});
									}
								});
							},
							fail: function(res) {}
						});
						console.log("login -> infoRes", infoRes);

						//   self.$go.post("/login", formdata).then(res => {
						//     //这是我封装的请求方法
						//     if (res.code == 200) {
						//       //登录成功
						//     }
						//   });
					},
					fail: function(res) {}
				});
			}
		}
	};
</script>
<style lang="scss" scoped>
	.logo {
		width: 280rpx;
		margin: 0 auto;
		display: block;
		margin-top: 30%;
	}

	.login-box {
		display: flex;
		flex-direction: column;
		white-space: nowrap;
		position: fixed;
		width: 100%;
		left: 0;
		bottom: 20%;
	}
</style>
